var PRO=PRO||{};
PRO.loanList=(function(){
	//动态绑定事件
	function bindEvents(){
		//设置贷款资料
		$(".btn-set-material-type").click(function(){
			var loanId=$(this).data("id");//获取贷款id
			var param={};
			param.loanId=loanId;
			var oldMaterials=[];
			 $.ajax({
					type : 'post',
					traditional: true,
					url : WEBROOT.ctx+'/sys/loan/loanMaterialtypeRe/getByLoanId',
					async : false, //默认为true 异步  
					data : param,
					cache : false,
					dataType : 'json',
					success : function(data) {
						oldMaterials=data;
					}
				});
			 	var submit = function (v, h, f) {
				if(v){
					return true;
				}
				//获取选择的资料类型
				var materialtypeIds =[]; 
				$('input[name="rootType"]:checked').each(function(index,element){ 
					materialtypeIds.push($(element).val()); 
				});
				var ret=false;
				param.materialtypeIds=materialtypeIds;
						   $.ajax({
							type : 'post',
							traditional: true,
							url : WEBROOT.ctx+'/sys/loan/loanMaterialtypeRe/ajaxSave',
							async : false, //默认为true 异步  
							data : param,
							cache : false,
							dataType : 'json',
							success : function(data) {
								ret=data;
							}
						});
				  var retText=ret?"设置成功":"设置失败";
		          return true;
				};
			$.jBox($("#tempalte_set_type").html(), {title:"设置贷款资料", submit: submit ,buttons:{"保存设置":false,"关闭":true}, 
				bottomText:"请勾选该贷款所需要的资料类型"});
			
			if(oldMaterials.length>0){
				var cbs=$("#jbox-content").find('input[name="rootType"]');
				console.log(cbs)
				console.log(oldMaterials)
				for ( var j = 0; j < cbs.length; j++) {
					$(cbs[i]).attr("checked", false);
					for ( var i = 0; i < oldMaterials.length; i++) {
							console.log(oldMaterials[i].materialTypeId+":"+$(cbs[j]).val()+"-"+j)
							if(oldMaterials[i].materialTypeId==$(cbs[j]).val()){
								$(cbs[j]).attr("checked", true);
							}
					}
				}
			}
		});
	}
	return {
		//页面初始化
		init:function(){
			bindEvents();
		}
	}
})();
$(function(){
	PRO.loanList.init();
})